package com.zhuanzhuan.service.impl;

import com.zhuanzhuan.dao.AdminDao;
import com.zhuanzhuan.dao.GoodsDao;
import com.zhuanzhuan.dao.TradelogDao;
import com.zhuanzhuan.dao.impl.AdminDaoMySQLImpl;
import com.zhuanzhuan.dao.impl.GoodsDaoMySQLImpl;
import com.zhuanzhuan.dao.impl.TradelogDaoMySQLImpl;
import com.zhuanzhuan.entity.Admin;
import com.zhuanzhuan.service.AdminService;
import com.zhuanzhuan.vo.GoodsDetail;
import com.zhuanzhuan.vo.TradelogDetail;

import java.util.List;
import java.util.Scanner;

public class AdminServiceImpl implements AdminService {

    //管理员登录
    @Override
    public Admin login(String name, String pwd) {
        //创建DAO对象
        AdminDao adminDao = new AdminDaoMySQLImpl();
        Admin admin = adminDao.getAdminByNameAndPwd(name, pwd);
        return admin;
    }

    //显示交易记录
    @Override
    public void listTradeLog() {
        //创建DAO对象
        TradelogDao tradelogDao = new TradelogDaoMySQLImpl();
        List<TradelogDetail> tradelogs = null;
        tradelogs = tradelogDao.getTradelogs();
        System.out.println("序号\t交易编号\t名称\t价格\t卖家\t买家\t交易时间");
        for (int i = 0; i < tradelogs.size(); i++) {
            TradelogDetail tradelog = tradelogs.get(i);
            System.out.println(i + 1 + "\t" +
                    tradelog.getId() + "\t" +
                    tradelog.getGoodsName() + "\t" +
                    tradelog.getPrice() + "\t" +
                    tradelog.getSellerName() + "\t" +
                    tradelog.getBuyerName() + "\t" +
                    tradelog.getTradeTime() + "\t");
        }
    }

    //显示好物信息
    @Override
    public void listGoods(int type) {
        int curPage = 0;
        //创建DAO对象
        GoodsDao goodsDao = new GoodsDaoMySQLImpl();
        Scanner input = new Scanner(System.in);
        boolean isShowGoods = true;
        List<GoodsDetail> listGoods = null;
        do{
            if(isShowGoods) {
                listGoods = goodsDao.listOnSellGoods(curPage,type);
                showListGoods(listGoods, curPage);
            }
            int pageCounts = (int) Math.ceil(goodsDao.getOnSellGoodsCountByType(type) / 5.0);
            //判断当前页是否为最后一页
            boolean hasLastPage = pageCounts > curPage + 1;
            System.out.println("1.查看好物详情\t2.上一页\t3.下一页\t0.返回上一级");
            System.out.print("请输入要执行的操作:");
            String action = input.next();
            //查看好物
            if (action.equals("1")) {
                System.out.print("请输入要查看的好物编号:");
                //计算好物在列表中的索引
                int index = Integer.valueOf(input.next()) - curPage*5;
                if (index <= listGoods.size() && index >= 1) {
                    GoodsDetail goods = listGoods.get(index-1);
                    showGoodsDetail(goods);
                    break;
                }else{
                    System.out.println("好物编号输入错误！");
                }
            }//显示上一页
            else if (action.equals("2")) {
                //当前页不是首页的情况
                isShowGoods = curPage > 0;
                if (isShowGoods) {
                    curPage--;
                } else {
                    System.out.println("已到首页，无法向前翻页！");
                }
            }//显示下一页
            else if (action.equals("3")) {
                //当前不是尾页的情况
                isShowGoods = hasLastPage;
                if (hasLastPage) {
                    curPage++;
                } else {
                    System.out.println("已到尾页，无法向后翻页！");
                }
            } else if (action.equals("0")) {
                break;
            } else {
                System.out.println("输入错误，请重新输入");
            }
        }while(true);
    }

    private void showListGoods(List<GoodsDetail> listGoods, int curPage){
        System.out.println("编号\t名称\t价格\t上架时间");
        for(int i = 0; i < listGoods.size(); i++){
            GoodsDetail goods = listGoods.get(i);
            //显示的编号当前页码有关
            int index = curPage*5+i+1;
            System.out.println(index+"\t"+
                    goods.getName()+"\t" +
                    goods.getPrice()+"\t" +
                    goods.getAddTime()+"\t");
        }
    }

    private void showGoodsDetail(GoodsDetail goods){
        System.out.println("好物名称:" + goods.getName());
        System.out.println("好物类型:" + goods.getType());
        System.out.println("好物价格:" + goods.getPrice());
        System.out.println("好物主人:" + goods.getOwner());
        System.out.println("好物介绍:" + goods.getDescription());
        System.out.println("上架时间:" + goods.getAddTime());
    }
}
